home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / demos / 57 / pascal / modular.doc < prev    next >
Encoding:
Text File  |  1986-09-18  |  5.0 KB  |  103 lines

  1. PERFORMING MODULAR COMPILATION WITH PERSONAL PASCAL
  2. ---------------------------------------------------
  3. One of the failings of standard Pascal is its lack of the ability to break a
  4. large program into smaller units which can be compiled separately.  Personal
  5. Pascal solves this deficiency by providing a rudimentary, yet powerful method
  6. of performing "modular compilation."  In this file, we will provide a simple
  7. example of using modules, as well as some guidelines and hints.  First, the
  8. example:
  9.  
  10. Consider the following simple program:
  11.  
  12. 1  PROGRAM simple;
  13. 2    VAR i: integer;
  14. 3    BEGIN
  15. 4      FOR i := 1 TO 10 DO writeln( i );
  16. 5    END.
  17.  
  18. Just for the purposes of this example, lets say we want to call a routine
  19. "print_message" instead of "writeln" in line 4.  We also want to put that
  20. routine into a different file so we can compile them separately.  We need to
  21. create two files, one of which will be our "module."  First, here is the
  22. "main file", which we will assume is called EXAMPLE.PAS:
  23.  
  24.   PROGRAM main_file;
  25.     VAR i: integer;
  26.  
  27.     { The next declaration tells the Pascal compiler that the "print_message"
  28.       routine will be inserted at link time.  That is, the EXTERNAL declaration
  29.       just tells Pascal what the routine looks like, but does not produce any
  30.       code just yet. }
  31.     PROCEDURE print_message( n: integer );
  32.       EXTERNAL;
  33.  
  34.     { Then the main routine is just like before: }
  35.     BEGIN
  36.       FOR i := 1 TO 10 DO
  37.         print_message( i );  { But we call print_message instead of writeln }
  38.     END.
  39.  
  40. Before we go on to the module, lets look at a few things.
  41.   1.  The modular compilation flag (M+) didn't appear anywhere in this file!
  42.       Why?  You use the M+ flag in each "module" file EXCEPT the one holding
  43.       your main routine.  Otherwise, you'll get link errors.
  44.   2.  We declared print_message just as we would have if we were going to code
  45.       it in this file, but instead of the body of the procedure, we just have
  46.       the directive EXTERNAL.
  47. Now we want to compile this main file to produce a file EXAMPLE.O, the "object"
  48. file.  But first, we must turn OFF the "Chain to linker" flag in the compiler
  49. options dialog box.  Also set the compiler to compile for TOS, since we're
  50. just using "writeln" to print to the screen.  Assuming that EXAMPLE compiled
  51. successfully, lets move on to the "module" file, which we'll call MODULE.PAS:
  52.  
  53.   {$M+,E+}  { This is a module, and we want its procedures to be visible }
  54.   PROGRAM module;
  55.  
  56.     PROCEDURE print_message( n: integer );
  57.       BEGIN
  58.         writeln( 'In the module with parameter ', n );
  59.       END;
  60.  
  61.     BEGIN
  62.     END.  { This main routine MUST be empty! }
  63.  
  64. If you type this in and compile it (again with "Chain to linker" OFF!), you
  65. will get a file MODULE.O.  Now we want to link both EXAMPLE.O and MODULE.O
  66. together with the Pascal libraries to produce a final program file.  Put the
  67. name "module.o" in the "Additional link files" fields of the linker options
  68. dialog box.  Then choose "Link file..." from the File menu, and select the
  69. file EXAMPLE.O.  The linker will first go to "example.o", then "module.o", then
  70. the libraries, in order to produce a final object file EXAMPLE.TOS, which you
  71. can run to see the results of our simple example.
  72.  
  73. A NOTE ON GLOBAL VARIABLES
  74. --------------------------
  75. In our sample module, we did not declare any global variables.  If we wanted
  76. to access the global variables that were declared in the main program (just
  77. the integer i, in this case), we would have had to declare ALL the global
  78. variables THE SAME WAY AND IN THE SAME ORDER AS THE MAIN PROGRAM.  In order to
  79. make this simpler, put all your global declarations into a file, then use the
  80. include directive to insert these into all your files (the main routine, too).
  81.  
  82. COMMENTARY
  83. ----------
  84. As you can tell, our example did not demonstrate any advantage of using modular
  85. compilation.  In fact, we went to more work that we would have by having just
  86. one source file!  In general, if your program if fairly small, you will not
  87. benefit from breaking your program up.  On the other hand, if your program is
  88. quite large, you can save a lot of compile time by splitting it up into several
  89. parts.  If possible, you should form the modules so that routines with similar
  90. functions are in the same module.  The Personal Pascal compiler was generated
  91. in this way.  It is formed of six modules, which together total to over 130K
  92. of program.  When you use modular compilation, keep the following points in
  93. mind:
  94.   -- be sure to turn OFF "Chain to linker"
  95.   -- Use the M+,E+ directives ONLY in modules, NOT in your main program
  96.   -- The main program segment in a module MUST be emtpy:
  97.        BEGIN
  98.        END.
  99.   -- If you want to access any global variables from modules, all global VAR
  100.      declarations must also be in the module.  We suggest putting your global
  101.      CONST, TYPE, and VAR declarations into a separate files, and just
  102.      include it in all modules AND in your main program.